type SetFunction = (key: string, value: number | string | any[] | object) => void;
type GetFunction = (key: string) => string | object;
interface Storage {
    setSession: SetFunction;
    setLocal: SetFunction;
    getSession: GetFunction;
    getLocal: GetFunction;
    clearSession: Function;
    clearLocal: Function;
}
const storage: Storage = {
    setSession: (key, value) => {
        const type = typeof value;
        if (type === 'string') {
            sessionStorage.setItem(key, `"${value}"`);
        } else {
            sessionStorage.setItem(key, JSON.stringify(value));
        }
    },
    getSession: (key) => {
        const ret = sessionStorage.getItem(key) || '';
        if (ret) {
            try {
                return JSON.parse(ret);
            } catch (e) {
                return ret;
            }
        }
        return '';
    },
    setLocal: (key, value) => {
        const type = typeof value;
        if (type === 'string') {
            localStorage.setItem(key, `"${value}"`);
        } else {
            localStorage.setItem(key, JSON.stringify(value));
        }
    },
    getLocal: (key) => {
        const ret = localStorage.getItem(key) || '';
        if (ret) {
            try {
                return JSON.parse(ret);
            } catch (e) {
                return ret;
            }
        }
        return '';
    },
    clearSession: (key?: string | undefined): void => {
        if (key) {
            sessionStorage.removeItem(key);
        } else {
            sessionStorage.clear();
        }
    },

    clearLocal: (key?: string | undefined): void => {
        if (key) {
            localStorage.removeItem(key);
        } else {
            localStorage.clear();
        }
    }
};
export default storage;
